package view.project;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.DateFormatter;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import dal.project.Project;
import dal.project.ProjectUtil;

/**
 * Servlet implementation class UpdateProject
 */
@WebServlet("/UpdateProject")
public class UpdateProject extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UpdateProject() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter printWriter=response.getWriter();
		
		response.setContentType("text/HTML");
		response.setHeader("Cache-control", "no-cache, no-store");
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Expires", "-1");

		response.setHeader("Access-Control-Allow-Origins", "*");
		response.setHeader("Access-Control-Allow-Methods", "POST");
		response.setHeader("Access-Control-Allow-Headers", "Content-Type");
		response.setHeader("Access-Control-Max-Age", "86400");

		String id = request.getParameter("projectId");
		String title = request.getParameter("projectTitle");
		String owner = request.getParameter("projectOwner");
		String location = request.getParameter("projectLocation");
		String date = request.getParameter("projectDate");
		System.out.println("date = "+date);
		Date sqlDate=null;
	    try {
	    	SimpleDateFormat dateFormatter=new SimpleDateFormat("yyyy-MM-dd");
			java.util.Date d=	dateFormatter.parse(date);
			sqlDate=new Date(d.getTime());
			
		} catch (ParseException e1) {
			
			e1.printStackTrace();
		}
		System.out.println("sql dat == "+sqlDate);
		
		

		Gson gson = new Gson();
		JsonObject myJsonObj = new JsonObject();
		String statusText="";
		
		Project project = new Project();
		project.setProjectId(id);
		project.setLocation(location);
		project.setDate(sqlDate);
		project.setOwner(owner);
		project.setTitle(title);

		try {

			boolean isOk = new ProjectUtil().update(project);
			if (isOk) {

				myJsonObj.addProperty("success", true);

				statusText = "<b>" + " Project Information Added Succesfully "
						+ "</b>";
			} else {

				myJsonObj.addProperty("success", false);

				statusText = "<b>"
						+ " Sorry ! Unable to add Project Information "
						+ "</b>";
			}

		} catch (Exception e) {
			myJsonObj.addProperty("success", false);
			statusText = "<b>"
					+ " Sorry ! Error occured while processing request "
					+ "</b>";
		}

		myJsonObj.addProperty("statusText", statusText);
		printWriter.println(myJsonObj.toString());
		printWriter.close();

	}

}
